<template>
	<view v-if="isload">
		<view style="height:var(--status-bar-height);"></view>

		<view class="logo">
			<image :src="logo"></image>
		</view>
		<view class="login-form">
			<view class="title"><text>申请获取以下权限</text></view>
			<view class="content"><text>获得您的公开信息(昵称，头像等)</text></view>
			<view class="butts">
				<view class="butt"><button type="default"
						style="background-image: linear-gradient(to right, #888888, #888888);"
						@click="onRefuse">拒绝授权</button></view>
				<view class="butt"><button @click="wxGetUserInfo" type="default">授权登录</button></view>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->


		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<u-form class="content">
			<u-form-item label="手机号" label-width="150">
				<u-input v-model="sub.mobile" type="number" :clearable="true" placeholder="请输入手机号" />
			</u-form-item>
			<u-form-item label="验证码" label-width="150">
				<u-input v-model="sub.code" type="number" :clearable="false" placeholder="请输入验证码" />
				<view slot="right">
					<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
					<view class='btn-code' @click="getCode">{{tips}}</view>
				</view>
			</u-form-item>
		</u-form>
		<view class="mt80">
			<u-button @click="submit" hover-class="none" class="btn-submit">登 录</u-button>
		</view>
		<view style="padding-bottom: 30rpx;">
			<copyRight class="mt80" />
		</view>
		<!-- #endif -->

	</view>
</template>
<script>
	export default {
		data() {
			return {
				isload: false,
				tips: '获取验证码',
				sub: {
					mobile: '',
					code: '',
				},
				logo: ''
			};
		},
		onLoad(options) {

			//#ifdef APP-PLUS  || MP-WEIXIN
			this.$api.getSite().then(res => {
				this.logo = res.data.logo
				this.isload = true
			});
			//#endif

			//#ifdef H5
			if (options.code) {
				this.$api.wechatLogin({ code: options.code }).then(res => {
					if (res.code == 1) {
						uni.setStorageSync('token', res.data.token);
						let retcurl = uni.getStorageSync('retcurl');
						if (!retcurl) {
							retcurl = 'pages/my/index';
						}
						uni.reLaunch({
							url: '/' + retcurl
						});

					}
				});
			} else {
				if (getApp().globalData.dev) {
					//调试登录-start
					this.$api.testLogin({ uid: 1 }).then(res => {
						uni.setStorageSync('token', res.data.token);
						let retcurl = uni.getStorageSync('retcurl');
						if (!retcurl) {
							retcurl = '/pages/pay/index';
						}
						uni.reLaunch({
							url: '/' + retcurl
						});
					});
				} else {
					this.$api.wechatLogin().then(res => {
						window.location.href = res.data;
					});
				}
			}
			//#endif
		},
		methods: {
			// #ifdef MP-WEIXIN
			//拒绝登录
			onRefuse() {
				uni.switchTab({
					url: '/pages/home/home'
				})
			},
			wxGetUserInfo() {
				let _this = this;
				_this.$api.testLogin({
					uid: 2
				}).then(res => {
					uni.setStorageSync('token', res.data.token);
					let retcurl = uni.getStorageSync('retcurl');
					if (!retcurl) {
						retcurl = 'pages/my/index';
					}
					uni.reLaunch({
						url: '/' + retcurl
					});
					console.log(res);
				})
				return
				uni.getUserProfile({
					provider: 'weixin',
					lang: 'zh_CN',
					desc: '用于完善会员资料',
					success: function(infoRes) {

						console.log(infoRes)

						let nickName = infoRes.userInfo.nickName; //昵称
						let avatarUrl = infoRes.userInfo.avatarUrl; //头像

						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								_this.$api.miniLogin({
									code: loginRes.code,
									nickName: nickName,
									avatarUrl: avatarUrl,
									encryptedData: infoRes.encryptedData,
									iv: infoRes.iv,
								}).then(res => {
									uni.setStorageSync('token', res.data.token);
									let retcurl = uni.getStorageSync('retcurl');
									if (!retcurl) {
										retcurl = 'pages/my/index';
									}
									uni.reLaunch({
										url: '/' + retcurl
									});
								});
							},
						});
					},
					fail(res) {}
				});
			},
			// #endif

			//#ifdef APP-PLUS
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				let reg = /^1[0-9]{10}$/;
				if (this.sub.mobile.length < 11 || !reg.test(this.sub.mobile)) {
					return this.$until.error('手机号不正确');
				}
				let _this = this;
				if (_this.$refs.uCode.canGetCode) {
					_this.$api.sms_send({
						mobile: _this.sub.mobile,
						event: 'login',
					}).then(res => {
						if (res.code == 1) {
							_this.$until.success(res.msg, function() {
								_this.$refs.uCode.start();
							});
						}
					})
				}
			},
			submit() {
				let reg = /^1[0-9]{10}$/;
				if (this.sub.mobile.length < 11 || !reg.test(this.sub.mobile)) {
					return this.$until.error('手机号不正确');
				}
				if (!this.sub.code) {
					return this.$until.error('验证码不能为空');
				}
				let _this = this;
				_this.$api.user_aplogin(this.sub).then(res => {

					if (res.code == 1) {
						_this.$until.success(res.msg, function() {
							uni.setStorageSync('token', res.data.token);
							let retcurl = uni.getStorageSync('retcurl');
							if (!retcurl) {
								retcurl = 'pages/my/index';
							}
							uni.reLaunch({
								url: '/' + retcurl
							});

						});
					}

				});
			},
			//#endif
		}
	};
</script>
<style lang="scss" scoped>
	.logo {
		width: 100%;
		text-align: center;
		padding-top: 80rpx;

		image {
			width: 200rpx;
			height: 200rpx;

		}
	}

	.login-form {
		background-color: #ffffff;
		border-radius: 25rpx;
		padding: 100rpx 20rpx 120rpx 60rpx;

		.title,
		.content {
			margin: 20rpx 0;

			text {
				font-size: 32rpx;
			}
		}

		.content text {
			color: #A39EA0;
		}

		.butts {
			width: 100%;
			display: flex;
			justify-content: space-between;
			margin-top: 50rpx;

			.butt {
				width: 300rpx;

				button {
					border-radius: 50rpx;
					color: #ffffff;
					background-image: linear-gradient(to right, #3889FF, #3889FF);
				}
			}
		}
	}
</style>
